module BiTree

# 二叉树节点
mutable struct BiNode
    value
    left::BiNode
    right::BiNode
    BiNode() = (x = new(nothing,); x.left = x; x.right = x) # 自引用
end

has_left(node::BiNode) = !(node.left === node)
has_right(node::BiNode) = !(node.right === node)
# 节点的度数
deg(node::BiNode) = Int(has_left(node)) + Int(has_right(node))

# 节点的级
# TODO: 规范化，该函数尚不正规
function rank(node::BiNode)
    lrank = has_left(node) ? rank(node.left) + 1 : 0
    rrank = has_right(node) ? rank(node.right) + 1 : 0
    return max(lrank, rrank)
end


function test()
    root = BiNode()
    root.value = 1
    n1 = BiNode()
    root.left = n1
    print("$(root.value) 度数：$(deg(root))，级数：$(rank(root))")
end

end